package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
@Transactional
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    @Resource
    private WmSensitiveMapper wmSensitiveMapper;

    @Override
    public ResponseResult<List<Map<String, Object>>> myList(SensitiveDto sensitiveDto) {
        String name = sensitiveDto.getName();
        Integer page = sensitiveDto.getPage();
        Integer size = sensitiveDto.getSize();
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(WmSensitive::getSensitives,name).orderByDesc(WmSensitive::getCreatedTime);
        IPage<WmSensitive> iPage = new Page<>();
        iPage.setCurrent(page);
        iPage.setSize(size);
        IPage<WmSensitive> selectPage = wmSensitiveMapper.selectPage(iPage, queryWrapper);
        List<Map<String, Object>> list = selectPage.getRecords().stream().map(s -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", s.getId());
            map.put("sensitives", s.getSensitives());
            Date createdTime = s.getCreatedTime();
            map.put("createdTime", createdTime.getTime());
            return map;
        }).collect(Collectors.toList());
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult<String> del(Integer id) {
        wmSensitiveMapper.deleteById(id);
        return ResponseResult.okResult("删除成功！");
    }

    @Override
    public ResponseResult<String> mySave(WmSensitive wmSensitive) {
        wmSensitiveMapper.insert(wmSensitive);
        return ResponseResult.okResult("添加成功！");
    }

    @Override
    public ResponseResult<String> myUpdate(WmSensitive wmSensitive) {
        wmSensitiveMapper.updateById(wmSensitive);
        return ResponseResult.okResult("修改成功！");
    }
}
